Ext.define('YXMES.sres.controller.DeviceSpotCheckSetupController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'sres_devicespotchecksetup',
		ref : 'sresdevicespotchecksetup'
	} ],
	requires : [ 'YXMES.sres.store.ResourceStore', 'YXMES.sres.store.ResTypeStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.sres.view.DeviceSpotCheckSetupView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'sres_devicespotchecksetup' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'sres_devicespotchecksetup #btnSearch' : {
				click : function() {
					return this.handle(this.onBtnSearchClick, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'sres_devicespotchecksetup' ? item : item.up('sres_devicespotchecksetup'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'ResourceStore')
			return this.getMainView().SresViewDeviceResourceServiceStore;
		if (name == 'ResTypeStore')
			return this.getMainView().BasViewCommonDataSetupDetailServiceStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SresViewDeviceResourceServiceStore = Ext.create('YXMES.sres.store.ResourceStore', {
			pageSize : 30
		});
		view.BasViewCommonDataSetupDetailServiceStore = Ext.create('YXMES.sres.store.ResTypeStore', {
			pageSize : 30
		});
		view.down('#grdRes').reconfigure(view.SresViewDeviceResourceServiceStore);
		view.down('#bar7').bindStore(view.SresViewDeviceResourceServiceStore);
		view.down('#cmbResType').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
	},
	onAfterrender : function(container, eOpts) {
		this.bind();
		// User defined afterrender
		this.loadResTypeStore({
		    groupName: 'RES_TYPE'
		});
	},
	onDestroy : function(container, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SresViewDeviceResourceServiceStore) {
			view.SresViewDeviceResourceServiceStore.destroyStore();
			view.SresViewDeviceResourceServiceStore = undefined;
		}
		if (view.BasViewCommonDataSetupDetailServiceStore) {
			view.BasViewCommonDataSetupDetailServiceStore.destroyStore();
			view.BasViewCommonDataSetupDetailServiceStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onBtnSearchClick : function(button, e, eOpts) {
		this.loadResourceStore(this.getItem('frmSearch').getValues());
	},
	loadResourceStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResourceStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	},
	loadResTypeStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ResTypeStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		store.loadPage(1, options);
	}
});